import request from "@/utils/request";

export const getPageByCommentCount = ({orderByCommentCount, currentPage, pageSize}, sortBy='comments', searchKeyword='', type='0') => {
  return request({
      url: `/posts/postDisplay?sortBy=${sortBy}&type=${type}`+(searchKeyword && `&searchKeyword=${searchKeyword}`),
      method: 'post',
      data: {
          orderByCommentCount,
          currentPage,
          pageSize,
      }
  });
}

/**
 * 获取帖子内容
 */
export const getPostContent = ({postId}) => {
    return request({
        url: `/posts/postContent/${postId}`,
        method: 'get',
    });
}

/**
 * 获取帖子对应评论
 *
 * @param postId
 * @param currentPage
 * @returns {*}
 */
export const getPostCommentsWithReplies = ({postId, currentPage}) => {
    return request({
        url: `/posts/postComments/${postId}/${currentPage}`,
        method: 'get',
    })
}
/**
 * 提交发帖
 *
 * @param data
 * @returns {*}
 */
export const savePost = (data) => {
    return request({
        url: '/posts/savePost',
        method: 'post',
        data: data,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

/**
 *提交评论
 *
 * @param unickname
 * @param content
 * @param pid
 * @returns {*}
 */
export const saveComment = ({unickname, content, pid}) => {
    return request({
        url: '/posts/saveComment',
        method: 'post',
        data: {
            unickname,
            content,
            pid
        }
    })
}

/**
 * 提交评论对应回复
 *
 * @param content
 * @param fromunickname
 * @param tounickname
 * @param cid
 * @returns {*}
 */
export const saveReply = ({content, fromunickname, tounickname, cid}) => {
    return request({
        url: '/posts/saveReply',
        method: 'post',
        data: {content, fromunickname, tounickname, cid}
    })
}

/**
 * 提交点赞
 *
 * @param unickname
 * @param objType 区分评论和回复
 * @param objId   评论或回复id
 */
export const like = ({unickname, objType, objId}) => {
    return request({
        url: '/posts/like',
        method: 'post',
        data: {
            unickname,
            objType,
            objId
        }
    })
}